package com.soriya.nestlive.entity;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

@Data
public class PageResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private PageResult() {}

    public PageResult(int totalCount, int pageSize, int totalPage, int currentPage, List<T> list) {
        this.totalCount = totalCount;
        this.pageSize = pageSize;
        this.totalPage = totalPage;
        this.currentPage = currentPage;
        this.list = list;
    }

    /**
     * 总记录数
     */
    private int totalCount;
    /**
     * 每页记录数
     */
    private int pageSize;
    /**
     * 总页数
     */
    private int totalPage;
    /**
     * 当前页数
     */
    private int currentPage;
    /**
     * 列表数据
     */
    private List<T> list;

    public static <T> PageResult<T> from(IPage<T> page) {
        PageResult<T> tPageResult = new PageResult<>();
        tPageResult.totalCount = (int) page.getTotal();
        tPageResult.pageSize = (int) page.getSize();
        tPageResult.totalPage = (int) page.getPages();
        tPageResult.currentPage = (int) page.getCurrent();
        tPageResult.list = page.getRecords();
        return tPageResult;
    }

}
